//
//  QuotaAnalysisView.m
//  cocoapodText
//
//  Created by apple on 16/9/10.
//  Copyright © 2016年 apple. All rights reserved.
//


#import "ShiPei.h"
#import "QuotaAnalysisView.h"
#import <Foundation/Foundation.h>

@interface QuotaAnalysisView()<FSMyUIDatePickerDelegate,UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)NSMutableArray *energyTypes;
@property(nonatomic,strong)NSMutableArray *conditionInfos;
@property(nonatomic,strong)NSMutableArray *queryDataInfos;
@end

@implementation QuotaAnalysisView

/**
 *  初始化视图属性
 *
 *  @param frame 视图frame
 *
 *  @return self
 */
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //定制View
        self.backgroundColor = [UIColor clearColor];
        self.userInteractionEnabled = YES; //设置为NO后，不再响应touch方法
        self.multipleTouchEnabled = YES;
        //控制子视图不能超出父视图的范围
        self.clipsToBounds = YES;
        //添加子视图
        [self subView];
    }
    return self;
}

//子视图懒加载
-(void)subView{
    //内容栏
    _AllBackView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, [ShiPei CGFloatMakeScale:TabChildContentW+10], kScreenH-CommonBarH-kStatusH-[ShiPei CGFloatMakeScale:FSCommonBottomH])];
    [self addSubview:_AllBackView];
    
    _pickerOverlay=[[UIView alloc]initWithFrame:CGRectMake(0, 0, [ShiPei CGFloatMakeScale:TabChildContentW+10], kScreenH-CommonBarH-kStatusH)];
    [_pickerOverlay setBackgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.17]];
    UITapGestureRecognizer *gestureRecognizer;
    gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                action:@selector(overlaycancel)];
    [_pickerOverlay addGestureRecognizer:gestureRecognizer];
    
    [self initEnergyAnalysisMenu];
    [self initEnergyQueryMenu];
    [self initShowChartView];
    [self initQueryTableView];
}

-(void)initEnergyAnalysisMenu{
    UIView *EnergyAnalysisMenu=[[UIView alloc]initWithFrame:CGRectMake(0, 0, [ShiPei CGFloatMakeScale:210],  kScreenH-CommonBarH-kStatusH)];
    [_AllBackView addSubview:EnergyAnalysisMenu];
    [EnergyAnalysisMenu setUserInteractionEnabled:YES];
    [EnergyAnalysisMenu setBackgroundColor:[UIColor whiteColor]];
    
    //表格顶部
    UIView *tableTopView=[[UIView alloc]initWithFrame:[ShiPei CGRectMakeScale:10 :15 :185 :35]];
    [EnergyAnalysisMenu addSubview:tableTopView];
    [tableTopView setUserInteractionEnabled:YES];
    [tableTopView setBackgroundColor:FUIColorFromRGB(0x84b6e7)];
    
    UILabel *tableTopLabel=[[UILabel alloc]initWithFrame:[ShiPei CGRectMakeScale:17.5 :0 :130 :35]];
    [tableTopView addSubview:tableTopLabel];
    [tableTopLabel setBackgroundColor:[UIColor clearColor]];
    [tableTopLabel setText:@"贵阳市"];
    [tableTopLabel setTextColor:FUIColorFromRGB(0x333333)];
    [tableTopLabel setFont:[UIFont boldSystemFontOfSize:[ShiPei CGFloatMakeFontScaleWithPX:26]]];
    
    UILabel *tableTopAddLabel=[[UILabel alloc]initWithFrame:[ShiPei CGRectMakeScale:147.5 :0 :37.5 :35]];
    [tableTopView addSubview:tableTopAddLabel];
    [tableTopAddLabel setBackgroundColor:[UIColor clearColor]];
    [tableTopAddLabel setText:@"+"];
    [tableTopAddLabel setTextAlignment:NSTextAlignmentCenter];
    [tableTopAddLabel setTextColor:[UIColor whiteColor]];
    [tableTopAddLabel setFont:[UIFont boldSystemFontOfSize:[ShiPei CGFloatMakeFontScaleWithPX:40]]];
    
    //表格
    _GYMeunTableView=[[UITableView alloc]initWithFrame:CGRectMake([ShiPei CGFloatMakeScale:10], [ShiPei CGFloatMakeScale:50], [ShiPei CGFloatMakeScale:185], kScreenH-CommonBarH-kStatusH-[ShiPei CGFloatMakeScale:FSChangeTrendMenuH+70])];
    [EnergyAnalysisMenu addSubview:_GYMeunTableView];
    [_GYMeunTableView setBackgroundColor:[UIColor clearColor]];
    [_GYMeunTableView setUserInteractionEnabled:YES];
    [_GYMeunTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [_GYMeunTableView setDelegate:self];
    [_GYMeunTableView setDataSource:self];
    
}

-(void)initEnergyQueryMenu{
    _rightAllBackView=[[UIView alloc] initWithFrame:CGRectMake([ShiPei CGFloatMakeScale:225], 0, [ShiPei CGFloatMakeScale:630], kScreenH-CommonBarH-kStatusH)];
    [_AllBackView addSubview:_rightAllBackView];
    [_rightAllBackView setUserInteractionEnabled:YES];
    [_rightAllBackView setBackgroundColor:[UIColor clearColor]];
    
    
    UIView *EnergyQueryMenu=[[UIView alloc]initWithFrame:[ShiPei CGRectMakeScale:0 :15 :630 :40]];
    [_rightAllBackView addSubview:EnergyQueryMenu];
    [EnergyQueryMenu setUserInteractionEnabled:YES];
    [EnergyQueryMenu setBackgroundColor:[UIColor whiteColor]];
    
    UILabel *energyTypeLabel=[[UILabel alloc]initWithFrame:[ShiPei CGRectMakeScale:15 :0 :62.5 :40]];
    [EnergyQueryMenu addSubview:energyTypeLabel];
    [energyTypeLabel setTextAlignment:NSTextAlignmentCenter];
    [energyTypeLabel setUserInteractionEnabled:YES];
    [energyTypeLabel setText:@"能源类型"];
    [energyTypeLabel setTextColor:FUIColorFromRGB(0x333333)];
    [energyTypeLabel setFont:[UIFont boldSystemFontOfSize:[ShiPei CGFloatMakeFontScaleWithPX:25]]];
    
    //能源类型选择
    _energyTypeMenu=[[UILabel alloc]initWithFrame:[ShiPei CGRectMakeScale:77.5 :7.5 :135/2 :25]];
    [EnergyQueryMenu addSubview:_energyTypeMenu];
    [_energyTypeMenu setTextAlignment:NSTextAlignmentCenter];
    [_energyTypeMenu setBackgroundColor:FUIColorFromRGB(0xe1e1e1)];
    [_energyTypeMenu.layer setMasksToBounds:YES];
    [_energyTypeMenu.layer setCornerRadius:10];
    [_energyTypeMenu setUserInteractionEnabled:YES];
    [_energyTypeMenu setText:@""];
    [_energyTypeMenu setTextColor:FUIColorFromRGB(0x1b75cc)];
    [_energyTypeMenu setFont:[UIFont boldSystemFontOfSize:[ShiPei CGFloatMakeFontScaleWithPX:25]]];
    
    UITapGestureRecognizer *singleTap0 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(buttonpress0:)];
    [_energyTypeMenu addGestureRecognizer:singleTap0];
    
    
    UILabel *showTypeLabel=[[UILabel alloc]initWithFrame:[ShiPei CGRectMakeScale:160 :0 :60 :40]];
    [EnergyQueryMenu addSubview:showTypeLabel];
    [showTypeLabel setTextAlignment:NSTextAlignmentCenter];
    [showTypeLabel setUserInteractionEnabled:YES];
    [showTypeLabel setText:@"显示类型"];
    [showTypeLabel setTextColor:FUIColorFromRGB(0x333333)];
    [showTypeLabel setFont:[UIFont boldSystemFontOfSize:[ShiPei CGFloatMakeFontScaleWithPX:25]]];
    
    //显示类型
    _showTypeMenu=[[UILabel alloc]initWithFrame:[ShiPei CGRectMakeScale:220 :7.5 :135/2 :25]];
    [EnergyQueryMenu addSubview:_showTypeMenu];
    [_showTypeMenu setTextAlignment:NSTextAlignmentCenter];
    [_showTypeMenu setUserInteractionEnabled:YES];
    [_showTypeMenu setBackgroundColor:FUIColorFromRGB(0xe1e1e1)];
    [_showTypeMenu.layer setMasksToBounds:YES];
    [_showTypeMenu.layer setCornerRadius:10];
    [_showTypeMenu setUserInteractionEnabled:YES];
    [_showTypeMenu setText:@""];
    [_showTypeMenu setTextColor:FUIColorFromRGB(0x1b75cc)];
    [_showTypeMenu setFont:[UIFont boldSystemFontOfSize:[ShiPei CGFloatMakeFontScaleWithPX:25]]];
    
    UITapGestureRecognizer *singleTap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(buttonpress1:)];
    [_showTypeMenu addGestureRecognizer:singleTap1];
    
    UILabel *startEndTimeLabel=[[UILabel alloc]initWithFrame:[ShiPei CGRectMakeScale:287.5 :0 :70 :40]];
    [EnergyQueryMenu addSubview:startEndTimeLabel];
    [startEndTimeLabel setTextAlignment:NSTextAlignmentCenter];
    [startEndTimeLabel setUserInteractionEnabled:YES];
    [startEndTimeLabel setText:@"起止时间"];
    [startEndTimeLabel setTextColor:FUIColorFromRGB(0x333333)];
    [startEndTimeLabel setFont:[UIFont boldSystemFontOfSize:[ShiPei CGFloatMakeFontScaleWithPX:25]]];
    
    //开始时间
    _startTimeMenu=[[UILabel alloc]initWithFrame:[ShiPei CGRectMakeScale:357.5 :7.5 :80 :25]];
    [EnergyQueryMenu addSubview:_startTimeMenu];
    [_startTimeMenu setTextAlignment:NSTextAlignmentCenter];
    [_startTimeMenu setUserInteractionEnabled:YES];
    [_startTimeMenu setBackgroundColor:FUIColorFromRGB(0xe1e1e1)];
    [_startTimeMenu.layer setMasksToBounds:YES];
    [_startTimeMenu.layer setCornerRadius:10];
    [_startTimeMenu setUserInteractionEnabled:YES];
    [_startTimeMenu setText:@""];
    [_startTimeMenu setTextColor:FUIColorFromRGB(0x1b75cc)];
    [_startTimeMenu setFont:[UIFont boldSystemFontOfSize:[ShiPei CGFloatMakeFontScaleWithPX:25]]];
    
    UITapGestureRecognizer *singleTap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(startTimeClick:)];
    [_startTimeMenu addGestureRecognizer:singleTap2];
    
    UILabel *jiangeLabel=[[UILabel alloc]initWithFrame:[ShiPei CGRectMakeScale:437.5 :0 :20 :40]];
    [EnergyQueryMenu addSubview:jiangeLabel];
    [jiangeLabel setTextAlignment:NSTextAlignmentCenter];
    [jiangeLabel setUserInteractionEnabled:YES];
    [jiangeLabel setText:@"至"];
    [jiangeLabel setTextColor:FUIColorFromRGB(0x333333)];
    [jiangeLabel setFont:[UIFont boldSystemFontOfSize:[ShiPei CGFloatMakeFontScaleWithPX:25]]];
    
    //结束时间
    _endTimeMenu=[[UILabel alloc]initWithFrame:[ShiPei CGRectMakeScale:457.5 :7.5 :80 :25]];
    [EnergyQueryMenu addSubview:_endTimeMenu];
    [_endTimeMenu setTextAlignment:NSTextAlignmentCenter];
    [_endTimeMenu setUserInteractionEnabled:YES];
    [_endTimeMenu setBackgroundColor:FUIColorFromRGB(0xe1e1e1)];
    [_endTimeMenu.layer setMasksToBounds:YES];
    [_endTimeMenu.layer setCornerRadius:10];
    [_endTimeMenu setUserInteractionEnabled:YES];
    [_endTimeMenu setText:@""];
    [_endTimeMenu setTextColor:FUIColorFromRGB(0x1b75cc)];
    [_endTimeMenu setFont:[UIFont boldSystemFontOfSize:[ShiPei CGFloatMakeFontScaleWithPX:25]]];
    
    UITapGestureRecognizer *singleTap3 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(endTimeClick:)];
    [_endTimeMenu addGestureRecognizer:singleTap3];
    
    UIButton *queryButton=[[UIButton alloc]initWithFrame:[ShiPei CGRectMakeScale:557.5 :15/2 :50 :25]];
    [EnergyQueryMenu addSubview:queryButton];
    [queryButton setBackgroundImage:[CommonTool createImageWithColor:FUIColorFromRGB(0x3388cb) Frame:[ShiPei CGRectMakeScale:0 :0 :50 :25]] forState:UIControlStateNormal];
    [queryButton setBackgroundImage:[CommonTool createImageWithColor:FUIColorFromRGB(0x639ed7)  Frame:[ShiPei CGRectMakeScale:0 :0 :50 :25]] forState:UIControlStateHighlighted];
    [queryButton.layer setMasksToBounds:YES];
    [queryButton.layer setCornerRadius:5];
    [queryButton setTitle:@"查询" forState:UIControlStateNormal];
    [queryButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [queryButton.titleLabel setFont:[UIFont boldSystemFontOfSize:[ShiPei CGFloatMakeFontScaleWithPX:26]]];
    [queryButton addTarget:self action:@selector(onClickQuery) forControlEvents:UIControlEventTouchUpInside];
    [self initPickerView];
}

-(void)initPickerView{
    _is_picker_show=NO;
    _datePicker=[[MyUIDatePicker alloc]initWithFrame:CGRectMake(0, kScreenH-CommonBarH-kStatusH-[ShiPei CGFloatMakeScale:250], [ShiPei CGFloatMakeScale:860], [ShiPei CGFloatMakeScale:250])];
    [_datePicker setDelegate:self];
    [_pickerOverlay addSubview:_datePicker];
}

-(void)onQueren:(MyUIDatePicker *)datePicker{
    
    //20160729（时间格式）
    NSDateFormatter *dateFormat = [ [NSDateFormatter alloc] init];
    //备注：日=1 月=2 年=3
    if ([_xslxType isEqualToString:@"3"]) {
        [dateFormat setDateFormat:@"yyyy"];
    }else if ([_xslxType isEqualToString:@"2"]) {
        [dateFormat setDateFormat:@"yyyyMM"];
    }
    NSString *newDateStr = [dateFormat stringFromDate:datePicker.datePicker.date];
    
    /**
     * energyType 能源类型
     * xslxType 显示类型
     * startTime 开始时间
     * endTime 结束时间
     */
    if ([_currentType isEqualToString:@"startTime"]) {
        _startTime=newDateStr;
        [self.startTimeMenu setText:newDateStr];
    }else if([_currentType isEqualToString:@"endTime"]){
        _endTime=newDateStr;
        [self.endTimeMenu setText:newDateStr];
    }
    [_pickerOverlay removeFromSuperview];
    _is_picker_show=NO;
    
}

-(void)overlaycancel{
    [self onCancel:_datePicker];
}

-(void)onCancel:(MyUIDatePicker *)datePicker{
    [_pickerOverlay removeFromSuperview];
    _is_picker_show=NO;
}

-(void)initShowChartView{
    _chartAndQueryTableBack=[[UIScrollView alloc]initWithFrame:CGRectMake(0, [ShiPei CGFloatMakeScale:70], [ShiPei CGFloatMakeScale:630], kScreenH-CommonBarH-kStatusH-[ShiPei CGFloatMakeScale:80])];
    [_rightAllBackView addSubview:_chartAndQueryTableBack];
    [_chartAndQueryTableBack setUserInteractionEnabled:YES];
    _chartAndQueryTableBack.showsVerticalScrollIndicator = FALSE;
    _chartAndQueryTableBack.showsHorizontalScrollIndicator = FALSE;
    _chartAndQueryTableBack.bounces = NO;
    [_chartAndQueryTableBack setBackgroundColor:[UIColor clearColor]];
    
    UIView *showChartBack=[[UIView alloc]initWithFrame:[ShiPei CGRectMakeScale:0 :0 :630 :325]];
    [_chartAndQueryTableBack addSubview:showChartBack];
    [showChartBack setUserInteractionEnabled:YES];
    [showChartBack setBackgroundColor:[UIColor whiteColor]];
    
    self.chartTitle=[[UILabel alloc]initWithFrame:[ShiPei CGRectMakeScale:30 :0 :550 :40]];
    [self.chartTitle setTextAlignment:NSTextAlignmentCenter];
    [self.chartTitle setText:@""];
    [self.chartTitle setFont:[UIFont boldSystemFontOfSize:[ShiPei CGFloatMakeFontScaleWithPX:20]]];
    [self.chartTitle setTextColor:FUIColorFromRGB(0x333333)];
    [self.chartTitle setBackgroundColor:[UIColor clearColor]];
    [showChartBack addSubview:self.chartTitle];
    
    //折线图
    self.energyLineChart=[[LineChartView alloc] initWithFrame:[ShiPei CGRectMakeScale:30 :40 :550 :250]];
    [showChartBack addSubview:_energyLineChart];
    [_energyLineChart setDelegate:self];
    _energyLineChart.backgroundColor = [UIColor whiteColor];
    [self initLineChartStyle:_energyLineChart];
}

-(void)initQueryTableView{
    _queryTableBack=[[UIScrollView alloc]initWithFrame:CGRectMake(0, [ShiPei CGFloatMakeScale:340], [ShiPei CGFloatMakeScale:630], kScreenH-CommonBarH-kStatusH-[ShiPei CGFloatMakeScale:420])];
    [_chartAndQueryTableBack addSubview:_queryTableBack];
    [_queryTableBack setUserInteractionEnabled:YES];
    _queryTableBack.showsVerticalScrollIndicator = FALSE;
    _queryTableBack.showsHorizontalScrollIndicator = FALSE;
    _queryTableBack.bounces = NO;
    [_queryTableBack setBackgroundColor:[UIColor whiteColor]];
    
    //表格顶部
    _queryTabletopView=[[UIView alloc]initWithFrame:[ShiPei CGRectMakeScale:0 :0 :630 :35]];
    [_queryTableBack addSubview:_queryTabletopView];
    [_queryTabletopView setBackgroundColor:FUIColorFromRGB(0x84b6e7)];
    queryDataQATableCellView *tableCellView=[[queryDataQATableCellView alloc] initWithFrame:[ShiPei CGRectMakeScale:0 :0 :TabChildContentW :35]];
    [_queryTabletopView addSubview:tableCellView];
    [tableCellView setBackgroundColor:[UIColor clearColor]];
    self.tableCellView=tableCellView;
    
    _queryTableView=[[UITableView alloc]initWithFrame:
                     CGRectMake(0, [ShiPei CGFloatMakeScale:35], [ShiPei CGFloatMakeScale:630], kScreenH-CommonBarH-kStatusH-[ShiPei CGFloatMakeScale:455])];
    [_queryTableBack addSubview:_queryTableView];
    _queryTableView.scrollEnabled =NO; //设置tableview 不能滚动
    _queryTableView.bounces=NO;//去掉弹簧效果
    [_queryTableView setUserInteractionEnabled:YES];
    [_queryTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [_queryTableView setDelegate:self];
    [_queryTableView setDataSource:self];
    [_queryTableView setBackgroundColor:[UIColor clearColor]];
    
}

//能源类型
-(void)setGYMenuEnergyTypeData:(NSArray *)array{
    _energyTypes=[NSMutableArray array];
    for (int i=0; i<[array count]; i++) {
        NSDictionary *temDic=[array objectAtIndex:i];
        [_energyTypes addObject:temDic];
    }
    //默认能源类型
    NSDictionary *temDic=[_energyTypes objectAtIndex:0];
    NSString *name=[CommonTool getStringFromNSDictionary:temDic key:@"name" defaultValue:@""];
    _energyType=[CommonTool getStringFromNSDictionary:temDic key:@"esCode" defaultValue:@""];
    [self.energyTypeMenu setText:name];
    if([_delegate respondsToSelector:@selector(onInitFristChartTableData:esCode:)]){
        [_delegate onInitFristChartTableData:self esCode:_energyType];
    }
}


//多组折线图
-(void)multipleLineChartDataWithLegend:(NSArray *)legend{
    //设置图标题
    [self.chartTitle setText:_title];
    
    [self setMultipleLineChartDataCount:_energyLineChart Legend:legend];
    //动画
    [_energyLineChart animateWithXAxisDuration:2.5 easingOption:ChartEasingOptionEaseInOutQuart];
}

-(void)setQueryTableData{
    if ([_queryDataInfos count]>0) {
        //获取查询横向宽度
        queryDataInfo *model=[_queryDataInfos objectAtIndex:0];
        CGFloat ContentW=[ShiPei CGFloatMakeScale:[model.infos count]*100+75];
        if (ContentW<[ShiPei CGFloatMakeScale:630]) {
            ContentW=[ShiPei CGFloatMakeScale:630];
        }
        //设置scrollview的contentSize
        CGFloat ContentH=[ShiPei CGFloatMakeScale:[_queryDataInfos count]*35+35];
        [self.queryTableBack setContentSize:CGSizeMake(ContentW, ContentH)];
        //设置scrollview的高度
        CGRect frame=self.queryTableBack.frame;
        frame.size.height=ContentH;
        [self.queryTableBack setFrame:frame];
        //设置表格宽高
        frame=self.queryTableView.frame;
        frame.size.width=ContentW;
        frame.size.height=ContentH-[ShiPei CGFloatMakeScale:35];
        [self.queryTableView setFrame:frame];
        //设置表格头部宽度
        frame=_queryTabletopView.frame;
        frame.size.width=ContentW;
        [self.queryTabletopView setFrame:frame];
        frame=_tableCellView.frame;
        frame.size.width=ContentW;
        [self.tableCellView setFrame:frame];
        
        //获取chart与查询表格的背景scrollview 的高度
        CGFloat chartWithQueryTableH=ContentH+[ShiPei CGFloatMakeScale:335];
        [self.chartAndQueryTableBack setContentSize:CGSizeMake([ShiPei CGFloatMakeScale:630], chartWithQueryTableH)];
        
        //刷新数据
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            // 处理耗时操作的代码块...
            //通知主线程刷新
            dispatch_async(dispatch_get_main_queue(), ^{
                //回调或者说是通知主线程刷新
                [_queryTableView reloadData];
            });
        });
    }
    
}

-(void)buttonpress0:(UITapGestureRecognizer *)recognizer{
    if (_energyTypes!=nil) {
        _currentType=@"energyType";
    
    NSMutableArray *models=[NSMutableArray array];
    for (int i=0; i<[_energyTypes count]; i++) {
        NSDictionary *temDic=[_energyTypes objectAtIndex:i];
        NSString *name=[CommonTool getStringFromNSDictionary:temDic key:@"name" defaultValue:@""];
        if (![name isEqualToString:@""]) {
            KxMenuItem *model;
            model=[KxMenuItem menuItem:name image:nil target:self action:@selector(KxMenuItemClick:)];
            [models addObject:model];
        }
    }
    
    [KxMenu setTitleFont:[UIFont boldSystemFontOfSize:[ShiPei CGFloatMakeFontScaleWithPX:25]]];
    Color color,color2;
    color.R=0;
    color.G=0;
    color.B=0;
    color2.R=1;
    color2.G=1;
    color2.B=1;
    OptionalConfiguration options;
    options.arrowSize=9;//指示箭头大小
    options.marginXSpacing= 7;  //MenuItem左右边距
    options.marginYSpacing= 9;  //MenuItem上下边距
    options.intervalSpacing= 25;  //MenuItemImage与MenuItemTitle的间距
    options.menuCornerRadius= 6.5;  //菜单圆角半径
    options.maskToBackground= true;  //是否添加覆盖在原View上的半透明遮罩
    options.shadowOfMenu= false;  //是否添加菜单阴影
    options.hasSeperatorLine= true;  //是否设置分割线
    options.seperatorLineHasInsets= false;  //是否在分割线两侧留下Insets
    options.textColor=color;  //menuItem字体颜色
    options.menuBackgroundColor= color2;  //菜单的底色
    [KxMenu showMenuInView:_rightAllBackView fromRect:recognizer.view.frame menuItems:models withOptions:options];
    }
}

-(void)buttonpress1:(UITapGestureRecognizer *)recognizer{
    _currentType=@"xslxType";
    NSMutableArray *models=[NSMutableArray array];
    KxMenuItem *model;
    //月=2 年=3
    model=[KxMenuItem menuItem:@"月" image:nil target:self action:@selector(KxMenuItemClick:)];
    [models addObject:model];
    
    model=[KxMenuItem menuItem:@"年" image:nil target:self action:@selector(KxMenuItemClick:)];
    [models addObject:model];
    
    [KxMenu setTitleFont:[UIFont boldSystemFontOfSize:[ShiPei CGFloatMakeFontScaleWithPX:25]]];
    Color color,color2;
    color.R=0;
    color.G=0;
    color.B=0;
    color2.R=1;
    color2.G=1;
    color2.B=1;
    OptionalConfiguration options;
    options.arrowSize=9;//指示箭头大小
    options.marginXSpacing= 7;  //MenuItem左右边距
    options.marginYSpacing= 9;  //MenuItem上下边距
    options.intervalSpacing= 25;  //MenuItemImage与MenuItemTitle的间距
    options.menuCornerRadius= 6.5;  //菜单圆角半径
    options.maskToBackground= true;  //是否添加覆盖在原View上的半透明遮罩
    options.shadowOfMenu= false;  //是否添加菜单阴影
    options.hasSeperatorLine= true;  //是否设置分割线
    options.seperatorLineHasInsets= false;  //是否在分割线两侧留下Insets
    options.textColor=color;  //menuItem字体颜色
    options.menuBackgroundColor= color2;  //菜单的底色
    [KxMenu showMenuInView:_rightAllBackView fromRect:recognizer.view.frame menuItems:models withOptions:options];
    
}
-(void)dismissMenu{
    [KxMenu dismissMenu];
}
-(void)KxMenuItemClick:(id)sender{
    KxMenuItem *model=sender;
    /**
     * energyType 能源类型
     * xslxType 显示类型
     * startTime 开始时间
     * endTime 结束时间
     */
    
    if ([_currentType isEqualToString:@"xslxType"]) {
        NSString *rule;
        //月=2 年=3
        _xslxType=@"2";
        rule=@"yyyyMM";
        if ([model.title isEqualToString:@"年"]) {
            _xslxType=@"3";
            rule=@"yyyy";
            _startTime=[CommonTool getDateTimeDifferenceWithYear:-5 Month:0 Day:0 Rule:rule];
            _endTime=[CommonTool getDateTimeDifferenceWithYear:-1 Month:0 Day:0 Rule:rule];
        }else if([model.title isEqualToString:@"月"]){
            _xslxType=@"2";
            rule=@"yyyyMM";
            _startTime=[CommonTool getDateTimeDifferenceWithYear:-1 Month:0 Day:0 Rule:rule];
            _endTime=[CommonTool getDateTimeDifferenceWithYear:0 Month:-1 Day:0 Rule:rule];
        }
        [_showTypeMenu setText:model.title];
        [_startTimeMenu setText:_startTime];
        [_endTimeMenu setText:_endTime];
    }else if ([_currentType isEqualToString:@"energyType"]) {
        for (int i=0; i<[_energyTypes count]; i++) {
            NSDictionary *temDic=[_energyTypes objectAtIndex:i];
            NSString *name=[CommonTool getStringFromNSDictionary:temDic key:@"name" defaultValue:@""];
            if ([model.title isEqualToString:name]) {
                _energyType=[CommonTool getStringFromNSDictionary:temDic key:@"esCode" defaultValue:@""];
                [self.energyTypeMenu setText:name];
                [self onClickQuery];
            }
        }
    }
    
}


-(void)startTimeClick:(id)sender{
    _currentType=@"startTime";
    if (!_is_picker_show) {
        [self addSubview:_pickerOverlay];
        _is_picker_show=YES;
    }
}

-(void)endTimeClick:(id)sender{
    _currentType=@"endTime";
    if (!_is_picker_show) {
        [self addSubview:_pickerOverlay];
        _is_picker_show=YES;
    }
}

-(void)onClickQuery{
    int i=0;
    if ([_energyTypeMenu.text isEqualToString:@""]) {
        i++;
        [CommonTool showAllTextDialogWithText:@"能源类型为空" parentView:_AllBackView];
    }else{
        if ([_showTypeMenu.text isEqualToString:@""]) {
            i++;
            [CommonTool showAllTextDialogWithText:@"显示类型为空" parentView:_AllBackView];
        }else{
            if ([_startTimeMenu.text isEqualToString:@""]) {
                i++;
                [CommonTool showAllTextDialogWithText:@"开始时间为空" parentView:_AllBackView];
            }else{
                if ([_endTimeMenu.text isEqualToString:@""]) {
                    i++;
                    [CommonTool showAllTextDialogWithText:@"结束时间为空" parentView:_AllBackView];
                }else{
                    NSDate *start;
                    NSDate *end;
                    if ([_xslxType isEqualToString:@"2"]) {
                        start = [CommonTool StringToDate:self.startTimeMenu.text rule:@"yyyyMM"];
                        end = [CommonTool StringToDate:self.endTimeMenu.text rule:@"yyyyMM"];
                        //按月查询不能超过12个月
                        NSDate *limtStartMonth=[CommonTool StringToDate:[CommonTool getDateTimeDifferenceWithYear:-1 Month:1 Day:0 Rule:@"yyyyMM" Date:end]  rule:@"yyyyMM"];
                        
                        NSTimeInterval timeBetween = [limtStartMonth timeIntervalSinceDate:start];
                        if (timeBetween>0) {
                            i++;
                            [CommonTool showAllTextDialogWithText:@"按月查询间隔时间不应超过12个月" parentView:self];
                        }
                    }else{
                        start = [CommonTool StringToDate:self.startTimeMenu.text rule:@"yyyy"];
                        end = [CommonTool StringToDate:self.endTimeMenu.text rule:@"yyyy"];
                        int xStart=[self.startTimeMenu.text intValue];
                        int xEnd=[self.endTimeMenu.text intValue];
                        if ((xEnd-xStart)>4) {
                            i++;
                            [CommonTool showAllTextDialogWithText:@"按年查询间隔时间不应超过5年" parentView:self];
                        }
                    }
                    
                    NSTimeInterval timeBetween = [end timeIntervalSinceDate:start];
                    if (timeBetween<0) {
                        i++;
                        [CommonTool showAllTextDialogWithText:@"终止时间小于开始时间" parentView:self];
                    }
                }
            }
        }
    }
    
    if (i==0) {
        if([_delegate respondsToSelector:@selector(onQueryChartData:esCode:xslx:startTime:endTine:orgid:structType:)]){
            
            [_delegate onQueryChartData:self esCode:_energyType xslx:_xslxType startTime:_startTime endTine:_endTime orgid:_orgId structType:@"4"];
        }
    }
}

-(void)setInitCondition{
    //默认显示类型为 月=2 年=3
    _xslxType=@"2";
    [_showTypeMenu setText:@"月"];
    //默认选择贵阳市
    _currentAreaName=@"贵阳市";
    _orgId=@"520100000:4:贵阳市:00";
    //查询时间
    _startTime=[CommonTool getDateTimeDifferenceWithYear:-1 Month:0 Day:0 Rule:@"yyyyMM"];
    _endTime=[CommonTool getDateTimeDifferenceWithYear:0 Month:-1 Day:0 Rule:@"yyyyMM"];
    [_startTimeMenu setText:_startTime];
    [_endTimeMenu setText:_endTime];
    _energyType=@"01";
    [_energyTypeMenu setText:@"电"];
}

//根据左侧菜单分类填充单选列表
-(void)setGYMenuTableData:(NSArray *)array{
    _conditionInfos=[NSMutableArray array];
    for (int i=1; i<[array count]; i++) {
        NSDictionary *temDic=[array objectAtIndex:i];
        NSString *name=[CommonTool getStringFromNSDictionary:temDic key:@"name" defaultValue:@""];
        NSString *areaId=[CommonTool getStringFromNSDictionary:temDic key:@"id" defaultValue:@""];
        energyConditionInfo *model;
        NSString *encode=[CommonTool getStringFromNSDictionary:temDic key:@"encode" defaultValue:@""];
        if (i%2==0) {
            model=[energyConditionInfo energyConditionInfoWithCondition:name isOdd:@"0" isSelect:@"0" flagNo:
                   areaId encode:encode];
        }else{
            model=[energyConditionInfo energyConditionInfoWithCondition:name isOdd:@"1"  isSelect:@"0" flagNo:
                   areaId encode:encode];
        }
        [_conditionInfos addObject:model];
    }
    //通知主线程刷新
    dispatch_async(dispatch_get_main_queue(), ^{
        //回调或者说是通知主线程刷新
        [self.GYMeunTableView reloadData];
    });
    
    //默认请求第一个选项的数据
    [self clickTableWithId:0];
}

-(void) setEnergyTableWithxAxis:(NSArray *)xAxis yAxiss:(NSArray *)yAxiss legend:(NSArray *)legend{
    NSMutableArray *models=[NSMutableArray array];
    
    NSArray *shiji=[[yAxiss objectAtIndex:0] objectForKey:@"data"];
    NSArray *zhibiao=[[yAxiss objectAtIndex:1] objectForKey:@"data"];
    NSArray *ratio=[[yAxiss objectAtIndex:1] objectForKey:@"ratio"];
    
    NSMutableArray *day=[NSMutableArray array];
    for (int i = 0; i < [xAxis count]; i++){
        [day addObject:[CommonTool getStringFromNSArray:shiji key:i]];
    }
    [models addObject:[queryDataQAInfo queryDataQAInfoWithItemName:[CommonTool getStringFromNSArray:legend key:0] isOdd:@"1" infos:day]];
    day=[NSMutableArray array];
    for (int i = 0; i < [xAxis count]; i++){
        [day addObject:[CommonTool getStringFromNSArray:zhibiao key:i]];
    }
    [models addObject:[queryDataQAInfo queryDataQAInfoWithItemName:[CommonTool getStringFromNSArray:legend key:1] isOdd:@"0" infos:day]];
    day=[NSMutableArray array];
    for (int i = 0; i < [xAxis count]; i++){
        [day addObject:[CommonTool getStringFromNSArray:ratio key:i]];
    }
    [models addObject:[queryDataQAInfo queryDataQAInfoWithItemName:@"完成率" isOdd:@"1" infos:day]];
    
    _queryDataInfos=[models copy];
    
    [self setQueryTableData];
    
    day=[NSMutableArray array];
    for (int i = 0; i < [xAxis count]; i++){
        [day addObject:[CommonTool getStringFromNSArray:xAxis key:i]];
    }
    //通知主线程刷新
    dispatch_async(dispatch_get_main_queue(), ^{
        //表格头部数据组装
        [_tableCellView setName:@"名称"];
        [_tableCellView setInfos:day];
        //回调或者说是通知主线程刷新
        [self.queryTableView reloadData];
    });
}

#pragma mark - 数据源方法
#pragma mark 返回分组数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

//设置rowHeight
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [ShiPei CGFloatMakeScale:35];
}

#pragma mark 返回每组行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView==self.GYMeunTableView) {
        return self.conditionInfos.count;
    }else{
        return self.queryDataInfos.count;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if (tableView==self.GYMeunTableView) {
        NSInteger x=indexPath.row;
        [self clickTableWithId:[[NSString stringWithFormat:@"%ld",(long)x] intValue]];
    }
}

-(void)clickTableWithId:(int )i{
    energyConditionInfo *model;
    model=[self.conditionInfos objectAtIndex:i];
    _currentAreaName=model.condition;
    //id:name:00
    NSString *temOrgId;
    temOrgId=[@"" stringByAppendingString:model.flagNo];
    temOrgId=[temOrgId stringByAppendingString:@":4"];
    temOrgId=[temOrgId stringByAppendingString:@":"];
    temOrgId=[temOrgId stringByAppendingString:model.condition];
    temOrgId=[temOrgId stringByAppendingString:@":"];
    temOrgId=[temOrgId stringByAppendingString:@"00"];
    _orgId=temOrgId;
    if ([_orgId isEqualToString:@""]) {
        _orgId=@"520100000:4:贵阳市:00";
    }
    NSLog(@"QA_orgid=%@",_orgId);
    //执行查询
    [self onClickQuery];
}

#pragma mark返回每行的单元格
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView==self.GYMeunTableView) {
        //1.到缓存中去取cell
        static NSString *ID=@"ID";
        //2.没有则创建cell
        areaTableRadioCell *cell=[tableView dequeueReusableCellWithIdentifier:ID];
        if (cell==nil) {
            cell=[[areaTableRadioCell
                   alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        }
        //3.设置cell的数据
        cell.areaInfo=self.conditionInfos[indexPath.row];
        //4.返回cell
        return cell;
    }else{
        //1.到缓存中去取cell
        static NSString *ID=@"ID1";
        //2.没有则创建cell
        queryDataQATableCell *cell=[tableView dequeueReusableCellWithIdentifier:ID];
        if (cell==nil) {
            cell=[[queryDataQATableCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        }
        //3.设置cell的数据
        cell.queryInfo=self.queryDataInfos[indexPath.row];
        //4.返回cell
        return cell;
    }
}


-(void)setData{
    if([_delegate respondsToSelector:@selector(onSetData:)]){
        [_delegate onSetData:self];
    }
}

#pragma mark - ChartViewDelegate

- (void)chartValueSelected:(ChartViewBase * __nonnull)chartView entry:(ChartDataEntry * __nonnull)entry dataSetIndex:(NSInteger)dataSetIndex highlight:(ChartHighlight * __nonnull)highlight
{
    //    [CommonTool showAllTextDialogWithText:[CommonTool keepDecimal:[NSString stringWithFormat:@"%f",highlight.value]] parentView:self.energyLineChart];
    [CommonTool showAllTextDialogInTopWithText:[CommonTool keepDecimal:[NSString stringWithFormat:@"%f",highlight.value]] parentView:self.energyLineChart];
}

- (void)chartValueNothingSelected:(ChartViewBase * __nonnull)chartView{
    //NSLog(@"chartValueNothingSelected");
}

- (void)setMultipleLineChartDataCount:(LineChartView *)chartView Legend:(NSArray *)legend{
    //设置数据
    NSMutableArray *dataSets = [[NSMutableArray alloc] init];
    double yMax=0;
    for (int i=0; i<[_yValsEnergyLine count]; i++) {
        LineChartDataSet *setTemp = [[LineChartDataSet alloc] initWithYVals:[_yValsEnergyLine objectAtIndex:i] label:[legend objectAtIndex:i]];
        NSArray *temArray=[_yValsEnergyLine objectAtIndex:i];
        for (int j=0; j<[temArray count]; j++) {
            ChartDataEntry *tem=[temArray objectAtIndex:j];
            double value=tem.value;
            if (value>yMax) {
                yMax=value;
            }
        }
        //设置折线为平滑曲线
        setTemp.drawCubicEnabled = NO;
        setTemp.cubicIntensity = 0.1;
        //设置折线为虚线
        //set1.lineDashLengths = @[@5.f, @2.5f];
        setTemp.highlightLineDashLengths = @[@5.f, @2.5f];
        //节点颜色设置
        [setTemp setColor:[_lineColors objectAtIndex:i]];
        //与节点颜色设置
        [setTemp setCircleColor:[_lineColors objectAtIndex:i]];
        //是否在拐点处显示数据
        setTemp.drawValuesEnabled = NO;
        //折线宽度
        setTemp.lineWidth = 1.0;
        //节点圆点半径
        setTemp.circleRadius = 4.0;
        //是否绘制中间的空心
        setTemp.drawCircleHoleEnabled = YES;
        setTemp.circleHoleColor=[UIColor whiteColor];
        //数字
        setTemp.valueFont = [UIFont systemFontOfSize:9.f];
        //设置折线下填充颜色渐变范围（前两位为透明值）
        NSArray *gradientColors  = @[
                                     (id)[ChartColorTemplates colorFromString:@"#001f73af"].CGColor,
                                     (id)[ChartColorTemplates colorFromString:@"#001f73af"].CGColor
                                     ];
        CGGradientRef gradient = CGGradientCreateWithColors(nil, (CFArrayRef)gradientColors, nil);
        setTemp.fillAlpha = 1.f;
        setTemp.fill = [ChartFill fillWithLinearGradient:gradient angle:90.f];
        setTemp.drawFilledEnabled = YES;
        CGGradientRelease(gradient);
        [dataSets addObject:setTemp];
    }
    //_energyLineChart.leftAxis.axisMaxValue=yMax;
    //_energyLineChart.rightAxis.axisMaxValue=yMax;
    
    LineChartData *data = [[LineChartData alloc] initWithXVals:_xValsEnergyLine dataSets:dataSets];
    chartView.data = data;
}

//线性图
-(void)initLineChartStyle:(LineChartView *)chartView{
    //描述
    chartView.descriptionText = @"";
    //无数据默认显示
    chartView.noDataTextDescription = @"";
    //网格背景
    chartView.pinchZoomEnabled = NO;
    chartView.drawGridBackgroundEnabled = NO;
    //启用拖拽图标
    chartView.dragEnabled = YES;
    //设置X与Y轴是否可缩放
    [chartView setScaleEnabled:NO];
    //X轴的显示位置，默认是显示在上面的
    chartView.xAxis.labelPosition = XAxisLabelPositionBottom;
    //不绘制网格线
    chartView.xAxis.drawGridLinesEnabled = NO;
    //不绘制限制线
    chartView.xAxis.drawLimitLinesBehindDataEnabled=NO;
    chartView.leftAxis.drawZeroLineEnabled = NO;
    chartView.leftAxis.drawLimitLinesBehindDataEnabled = NO;
    chartView.leftAxis.axisMinValue = 0;//设置Y轴的最小值
    chartView.leftAxis.startAtZeroEnabled = YES;//从0开始绘制
    
    chartView.rightAxis.drawZeroLineEnabled = NO;
    chartView.rightAxis.drawLimitLinesBehindDataEnabled = NO;
    chartView.rightAxis.axisMinValue = 0;//设置Y轴的最小值
    chartView.rightAxis.startAtZeroEnabled = YES;//从0开始绘制
    chartView.rightAxis.enabled = YES;
    /********************/
    chartView.legend.form = ChartLegendFormLine;
    //图例在柱状图中的位置
    chartView.legend.position = ChartLegendPositionBelowChartLeft;
    //图示样式: 方形、线条、圆形
    chartView.legend.form = ChartLegendFormSquare;
    //图示大小
    chartView.legend.formSize = 9.0;
    //字体大小
    chartView.legend.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:11.f];
    //间隔
    chartView.legend.xEntrySpace = 4.0;
}

@end
